<!DOCTYPE html>
<html>
    <head>
        <title>Test multiple calls to MediaKeySession.close()</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            // This test verifies that Chrome handles multiple close() calls
            // correctly. Currently the EME spec has the check for session
            // already closed happen immediately when the method is called,
            // but the action is performed asynchronously.
            // https://github.com/w3c/encrypted-media/issues/365

            async_test(function(test)
            {
                var initDataType;
                var initData;
                var mediaKeySession;

                navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                    initDataType = access.getConfiguration().initDataTypes[0];
                    initData = getInitData(initDataType);
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    mediaKeySession = mediaKeys.createSession();
                    return mediaKeySession.generateRequest(initDataType, initData);
                }).then(function() {
                    // Call close() multiple times.
                    return Promise.all([mediaKeySession.close(), mediaKeySession.close(), mediaKeySession.close()]);
                }).then(function() {
                    test.done();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error);
                });
            }, 'Test multiple calls to MediaKeySession.close().');
        </script>
    </body>
</html>
